package com.example.mealplanner;

import com.example.mealplanner.R;
import com.example.mealplanner.login.LoginActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterNewUser extends Activity {

	private String mPhone;
	private String mPassword;
	private String mName;
	private String mConfirm;

	// UI references.
	private EditText mPhoneView;
	private EditText mNameView;
	private EditText mPasswordView;
	private EditText mConfirmView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_new_user);
		
		// Set up the login form.
		mPhoneView = (EditText) findViewById(R.id.reg_phone);
		mPasswordView = (EditText) findViewById(R.id.reg_password);
		mNameView = (EditText) findViewById(R.id.reg_name);
		mConfirmView = (EditText) findViewById(R.id.reg_confirm);

		findViewById(R.id.register_button).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						attemptRegister();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register_new_user, menu);
		return true;
	}

	public void attemptRegister(){

		// Store values at the time of the login attempt.
		mPhone = mPhoneView.getText().toString();
		mPassword = mPasswordView.getText().toString();
		mName = mNameView.getText().toString();
		mConfirm = mConfirmView.getText().toString();

		boolean cancel = false;
		View focusView = null;

		// Check for a valid password.
		if(TextUtils.isEmpty(mPhone)){
			mPhoneView.setError(getString(R.string.error_field_required));
			focusView = mPhoneView;
			cancel = true;
		} else if (TextUtils.isEmpty(mName)) {
			mNameView.setError(getString(R.string.error_field_required));
			focusView = mNameView;
			cancel = true;
		} else if (TextUtils.isEmpty(mPassword)) {
			mPasswordView.setError(getString(R.string.error_field_required));
			focusView = mPasswordView;
			cancel = true;
		}else if (TextUtils.isEmpty(mConfirm)) {
			mConfirmView.setError(getString(R.string.error_field_required));
			focusView = mConfirmView;
			cancel = true;
		}else if (mPassword.length() < 4) {
			mPasswordView.setError(getString(R.string.error_invalid_password));
			focusView = mPasswordView;
			cancel = true;
		} else if(!mPassword.equals(mConfirm)){
			mConfirmView.setError("Confirm password not correct!");
			focusView = mConfirmView;
			cancel = true;
		}
		
		if (cancel) {
			// There was an error; don't attempt login and focus the first
			// form field with an error.
			focusView.requestFocus();
		} else {
			// Show a progress spinner, and kick off a background task to
			// perform the user login attempt.
			String message = "JSON object: {id : " + mPhone + ", " 
							+ "password : " + mPassword + ", " 
							+ "name : " + mName + "}";
			Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
			
			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			
		}
	}
}
